import {
    reqFindOrder,
    reqCart,
    reqUserAddRess,
    reqSubmitOrder,
    reqPayInfo,
    reqPayResut
} from '../../api/index';
import moment from 'moment'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsId: '', //存储立即购买的ID,但是购物车而来没有
        blessing: '', //祝福语
        cartList: [], //订单数组
        total: 0,
        //收件人地址
        address: {},
        buyName: "",
        buyPhone: '',
        //提交订单需要收集的参数
        orderInfo: {
            buyName: '', //收件人名字
            buyPhone: '', //收件人收集号码
            cartList: [], //购物列表
            deliveryDate: '', //日期
            remarks: '', //备注
            userAddressId: '', //送货地址ID
        },
        show: false, //控制底部日期
        minDate: new Date().getTime(),
        deliveryDate: '', // 期望送达日期
        //订单iD
        orderId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        options.goodsId && this.setData({
            goodsId: options.goodsId,
            blessing: options.blessing
        });
        if (options.goodsId) {
            //获取支付商品详情
            this.getGoodsInfo();
        } else {
            //购物车而来
            this.getCart();
        }
    },
    onShow(){
      //获取收件人信息地址
      this.getAddress();
    },
    async getGoodsInfo() {
        const {
            goodsId,
            blessing
        } = this.data;
        let result = await reqFindOrder({
            goodsId,
            blessing
        });
        if (result.code == 200) {
            this.setData({
                cartList: result.data.cartVoList
            })
            //计算总价
            this.computedTotal();
        }

    },
    //获取购物车支付信息
    async getCart() {
        let result = await reqCart();
        if (result.code == 200) {
            this.setData({
                cartList: result.data
            })
            //计算总价
            this.computedTotal();
        }

    },
    computedTotal() {
        let total = this.data.cartList.reduce((prev, next) => {
            return prev += next.count * next.price;
        }, 0);
        this.setData({
            total
        })
    },
    //获取收件人地址
    async getAddress() {
        let result = await reqUserAddRess();
        this.setData({
            address: result.data
        })
    },
    //提交订单
    async submitOrder() {
        // this.setData({
        //     orderInfo: {
        //         buyName: this.data.buyName,
        //         buyPhone: this.data.buyPhone,
        //         cartList: this.data.cartList,
        //         deliveryDate: this.data.deliveryDate,
        //         remarks: this.data.blessing,
        //         userAddressId: this.data.address.id
        //     }
        // });

        const params = {
            buyName: this.data.buyName,
            buyPhone: this.data.buyPhone,
            cartList: this.data.cartList,
            deliveryDate: this.data.deliveryDate,
            remarks: this.data.blessing,
            userAddressId: this.data.address.id
        }
        //提交订单
        let result = await reqSubmitOrder(params);
        if (result.code == 200) {
            this.setData({
                orderId: result.data
            });
            //获取支付信息
            this.getPayInfo();
        }

    },
    //弹出pop
    handleShowPopup() {
        this.setData({
            show: true
        })
    },
    //日历的确定
    confirm(value) {
        const time = moment(value.detail).format('YYYY/MM/DD');
        this.setData({
            //存储收件人预定的日期
            deliveryDate: time,
            show: false
        })
    },
    //取消
    cancel() {
        this.setData({
            show: false
        })
    },
    //获取支付信息
    async getPayInfo() {
        let result = await reqPayInfo(this.data.orderId);
        //支付
        console.log(result.data.timeStamp);
        if (result.code == 200) {
            console.log(123);
            wx.requestPayment({
                nonceStr: result.data.nonceStr,
                package: result.data.package,
                paySign: result.data.paySign,
                signType: result.data.signType,
                timeStamp: result.data.timeStamp,
                success: async () => {
                    let result = await reqPayResut(this.data.orderId);
                   
                    if(result.code==200){
                         wx.navigateTo({
                           url: '/pages/result/result',
                         })
                    }
                },
                fail: () => {

                }
            })
        }
    }
})